package mhl.service;

import java.util.List;
import java.util.UUID;

import mhl.dao.billDao;
import mhl.dao.muliTablebean_b_mDao;
import mhl.javabean.bill;
import mhl.javabean.menu;
import mhl.javabean.muliTablebean_b_m;

public class billService {
    billDao billDao = new billDao();
    // 添加另外一个service
    menuService menuService = new menuService();
    diningTableService diningTableService = new diningTableService();
    muliTablebean_b_mDao muliTablebean_b_mDao = new muliTablebean_b_mDao();

    /**
     * 生成账单方法,更新对应餐桌状态
     * 
     * @param menuId
     * @param nums
     * @param diningTable_id
     * @return
     */
    public boolean orderMenu(int menuId, int nums, int diningTable_id) {
        // 生成账单编号
        String billid = UUID.randomUUID().toString();

        menu menu = menuService.getMenuId(menuId);
        Double money = menu.getPrice() * nums;

        String sql = "insert into bill values(null,?,?,?,?,?,now(),'未结账') ";
        int row = billDao.dml(sql, billid, menuId, nums, money, diningTable_id);

        if (row <= 0) {
            return false;
        }
        // 更新餐桌状态
        int row2 = diningTableService.updateState(diningTable_id, "用餐中");

        if (row2 <= 0) {
            return false;
        }

        return true;
    }

    /**
     * 返回对应桌号的账单表所有内容
     * 
     * @param menuId
     * @return
     */
    public List<bill> showBill(int Table_id) {
        String sql = "select * from bill where diningTableID = ?";
        return billDao.queryMulit(sql, bill.class, Table_id);
    }
    
   /**
    * 查看所有餐桌账单(多表查询)
    * @return
    */
    public List<muliTablebean_b_m> showBill2() {
        String sql = "select bill.* , menu.name from bill , menu where bill.menuID = menu.id";
        return muliTablebean_b_mDao.queryMulit(sql, muliTablebean_b_m.class);
    }

    /**
     * 查询餐桌是否未结账方法
     * 
     * @param Table_Id
     */
    public boolean unPaydiningTable(int Table_Id) {
        String sql = "select * from bill where diningTableID = ? and state = '未结账' limit 0,1 ";
        bill bill = billDao.query(sql, bill.class, Table_Id);
        return bill != null;
    }

    /**
     * 结账方法
     * 
     * @param Table_id
     * @param paymode
     * @return
     */
    public boolean paying(int Table_id, String paymode) {
        String sql1 = "update bill set state = ? where diningTableID = ? and state = '未结账'";

        int row = billDao.dml(sql1, paymode, Table_id);

        if (row <= 0) {
            return false;
        }
        if (!diningTableService.UnsubscribeTable(Table_id)) {
            return false;
        }

        return true;

    }

}
